package cn.edu.szh.view;

import cn.edu.szh.bean.ClassInfo;
import cn.edu.szh.bean.GradeInfo;
import cn.edu.szh.bean.SubjectInfo;
import cn.edu.szh.dao.ClassDao;
import cn.edu.szh.dao.GradeDao;
import cn.edu.szh.dao.SubjectDao;

import javax.swing.*;
import java.awt.*;

abstract public class SelectBaseUI extends JFrame {
    JComboBox  comboBoxGrade=new JComboBox();
    JComboBox  comboBoxClass=new JComboBox();
    JComboBox  comboBoxSubject=new JComboBox();
    public SelectBaseUI() {
        this.setTitle("选择年级,班级,科目");
        this.setSize(400, 300);
        setLayout(new GridLayout(7,5,5,10));
        addBlank(5);

        addBlank(1);
        add(new JLabel("年级"));
        addBlank(1);
        add(comboBoxGrade);
        comboBoxGrade.addItem("请选择");
        addBlank(1);
        GradeDao gradeDao = new GradeDao();
        for (GradeInfo gradeInfo  : gradeDao.getAll()) {
            comboBoxGrade.addItem(gradeInfo);
        }
        ClassDao  classDao = new ClassDao();
        comboBoxGrade.addActionListener(e -> {
            comboBoxClass.removeAllItems();
            comboBoxClass.addItem("请选择");
            if ("请选择".equals(comboBoxGrade.getSelectedItem())) {
                return;
            }
            GradeInfo gradeInfo  = (GradeInfo) comboBoxGrade.getSelectedItem();
            int id = gradeInfo.getGrade_id();
            for (ClassInfo classInfo : classDao.getAll()) {
                if (classInfo.getGrade_id() == id) {
                    comboBoxClass.addItem(classInfo);
                }
            }
        });



        addBlank(1);
        add(new JLabel("班级"));
        addBlank(1);
        add(comboBoxClass);
        comboBoxClass.addItem("请选择");
        addBlank(1);

        ClassDao classDao1 = new ClassDao();
        for (ClassInfo classInfo : classDao1.getAll()) {
            comboBoxClass.addItem(classInfo);
        }
        SubjectDao subjectDao = new SubjectDao();
        comboBoxClass.addActionListener(e -> {
            comboBoxSubject.removeAllItems();
            comboBoxSubject.addItem("请选择");
            Object selectedItem = comboBoxClass.getSelectedItem();

            if ("请选择".equals(selectedItem)) {
                return;
            }

            if (selectedItem instanceof ClassInfo) {
                ClassInfo classInfo = (ClassInfo) selectedItem;
                int id1 = classInfo.getClass_id();


                for (SubjectInfo subjectInfo : subjectDao.getAll()) {
                    if (subjectInfo.getClass_id()== id1) {
                        comboBoxSubject.addItem(subjectInfo);
                    }
                }
            }
        });


        addBlank(1);
        add(new JLabel("科目"));
        addBlank(1);
        add(comboBoxSubject);
        comboBoxSubject.addItem("请选择");

        addBlank(1);

        addBlank(5);

        addBlank(2);
        JButton  button1=new JButton("确定");
        add(button1);
        button1.setEnabled(false);
        comboBoxClass.addActionListener(e -> {
        if ("请选择".equals(comboBoxClass.getSelectedItem())|| "请选择".equals(comboBoxSubject.getSelectedItem())) {
                button1.setEnabled(false);
            } else {
                button1.setEnabled(true);
            }
        });
        comboBoxSubject.addActionListener(e -> {
            if ("请选择".equals(comboBoxClass.getSelectedItem())|| "请选择".equals(comboBoxSubject.getSelectedItem())) {
                button1.setEnabled(false);
            } else {
                button1.setEnabled(true);
            }
        });
        button1.addActionListener(e -> {
            comit((GradeInfo) comboBoxGrade.getSelectedItem(),(ClassInfo) comboBoxClass.getSelectedItem(),(SubjectInfo) comboBoxSubject.getSelectedItem());
        });
        JButton  button2=new JButton("取消");
        add(button2);
        button2.addActionListener(e -> {
            this.dispose();
        });
        addBlank(1);

        addBlank(5);

    }

    abstract public void comit(GradeInfo  grade, ClassInfo class1,  SubjectInfo subjectInfo);
    private void addBlank(int num) {
        for (int i = 0; i < num; i++) {
            add(new JLabel());
        }
    }
}

